/**
 * 
 */

package com.chengyu.eyc.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.hutool.core.util.StrUtil;

public final class RegexUtils {
   
    public static String changeCharset(String str, String newCharset) {
        try {
            if (str != null) {
                // 用默认字符编码解码字符串。
                byte[] bs = str.getBytes();
                // 用新的字符编码生成字符串
                return new String(bs, newCharset);
            }
        } catch (Exception e) {
            e.getStackTrace();
        }

        return null;
    }

 
    public static boolean isContainBlankSpace(String str) {
        return !match("^[^\\s]*$", str);
    }

    public static boolean isContainMatcher(String regEx, String str) {
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * 验证输入是否是数字
     * 
     * @param 待验证的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isDecimal(String str) {
        String regex = "^[0-9]+(.[0-9]{0,})?$";
        return match(regex, str);
    }

    public static boolean isEmail(String email) {
        String str = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 验证非零的正整数
     * 
     * @param 待验证的字符串
     * @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
     */
    public static boolean isIntNumber(String str) {
        String regex = "^\\+?[1-9][0-9]*$";
        return match(regex, str);
    }

    public static boolean isLetterDigit(String str) {
        String regex = "^[a-zA-Z0-9]*$";
        return match(regex, str);
    }

    public static boolean isMobile(final String str) {
        Pattern p = Pattern.compile("^[1][3-9][0-9]{9}$");
        Matcher m = p.matcher(str);
        return m.matches();
    }

    public static boolean isNumber(String str) {
        String regex = "^[0-9]*$";
        return match(regex, str);
    }

    public static boolean isPositiveDecimal(String str, int posLen, int decLen) {
        String regex = null;
        if (posLen == 1) {
            regex = "^(0|[1-9]})(\\.[0-9]{1," + decLen + "})?$";
        } else {
            regex = "^(0|[1-9][0-9]{0," + (posLen - 1) + "})(\\.[0-9]{1," + decLen + "})?$";
        }
        return match(regex, str);
    }

    public static boolean isPositiveNumber(String str, int posLen) {
        String regex = null;
        if (posLen == 1) {
            regex = "^(0|[1-9]})?$";
        } else {
            regex = "^(0|[1-9][0-9]{0," + (posLen - 1) + "})?$";
        }
        return match(regex, str);
    }

    public static boolean isValidFileNme(String fileNme) {
        return match("^[a-zA-Z0-9](?:[a-zA-Z0-9 ._-]*[a-zA-Z0-9])?\\.[a-zA-Z0-9_-]+$", fileNme);
    }

    public static boolean isWblNum(String str) {
        String regex = "^(Q|D|K|W})([0-9]{9})$";
        return match(regex, str);
    }

    public static boolean match(String regex, String str) {
        if (StrUtil.isEmpty(regex) || StrUtil.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static String stringToUnicode(String str) {
        StringBuilder sb = new StringBuilder();
        char[] c = str.toCharArray();
        for (int i = 0; i < c.length; i++) {
            sb.append("\\u" + Integer.toHexString(c[i]));
        }
        return sb.toString();
    }

    /**
     * unicode转字符串
     * 
     * @param unicode
     * @return
     */
    public static String unicodeToString(String unicode) {
        StringBuilder sb = new StringBuilder();
        String[] hex = unicode.split("\\\\u");
        for (int i = 1; i < hex.length; i++) {
            int index = Integer.parseInt(hex[i], 16);
            sb.append((char)index);
        }
        return sb.toString();
    }

    private RegexUtils() {

    }

}
